package com.eight.lesson5;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.eight.lesson4.Dish;
import com.eight.lesson4.Dish.Type;
import com.eight.lesson4.DishList;

public class DishTest {

	public static void main(String[] args) {
		List<Dish> vegetarianMenu = DishList
				.getDishs().stream()
				.filter(Dish::isVegetarian)
				.collect(Collectors.toList());
		System.out.println(vegetarianMenu);
		
		// 筛选各异的元素
		System.out.println("***************筛选各异的元素***************");
		List<Integer> numbers = Arrays.asList(1,2,1,3,3,2,4);
		numbers.stream()
		.filter(i -> i%2==0)
		.distinct()
		.forEach(System.out::println);
		
		// 截短流
		System.out.println("***************截短流***************");
		DishList
		.getDishs().stream()
		.filter(d -> d.getCalories()>300)
		.limit(3)
		.forEach(System.out::println);
		
		// 跳过元素
		System.out.println("***************跳过元素***************");
		DishList
		.getDishs().stream()
		.filter(d -> d.getCalories()>300)
		.skip(3)
		.forEach(System.out::println);
		
		// 筛选前两个荤菜
		System.out.println("***************筛选前两个荤菜***************");
		DishList
		.getDishs().stream()
		.filter(d -> d.getType() == Type.MEAT)
		.limit(2)
		.forEach(System.out::println);
		
		// 获取菜名
		System.out.println("***************获取菜名***************");
		List<String> names = DishList
				.getDishs().stream()
				.map(Dish::getName)
				.collect(Collectors.toList());
		System.out.println(names);
		
		// 获取菜名长度
		System.out.println("***************获取菜名长度***************");
		List<Integer> len = DishList
				.getDishs().stream()
				.map(Dish::getName)
				.map(String::length)
				.collect(Collectors.toList());
		System.out.println(len);
		
		System.out.println("***********************************");
		String[] arrayOfWords = {"Goodbye", "World"};
		Stream<String> streamOfWords = Arrays.stream(arrayOfWords);
		streamOfWords.forEach(System.out::println);
		
		System.out.println("***********************************");
		List<String> words = new ArrayList<String>();
		words.add("Hello");
		words.add("World");
		words.stream()
		.map(w -> w.split(""))
		.flatMap(Arrays::stream)
		.distinct()
		.forEach(System.out::println);
		
		System.out.println("************************************");
		List<Integer> numberss = Arrays.asList(1,2,3,4,5);
		numberss.stream()
		.map(i -> i*i)
		.forEach(System.out::println);
		
		System.out.println("************************************");
		List<Integer> number1 = Arrays.asList(1,2,3);
		List<Integer> number2 = Arrays.asList(3,4);
		List<int[]> pairs = number1.stream()
				.flatMap(i -> number2.stream()
						.map(j -> new int[] {i,j}))
				.collect(Collectors.toList());
		for (int[] pair : pairs) {
			System.out.println("("+pair[0]+","+pair[1]+")");
		}
		
		System.out.println("************************************");
		List<Integer> number3 = Arrays.asList(1,2,3);
		List<Integer> number4 = Arrays.asList(3,4);
		List<int[]> pairs2 = number3.stream()
				.flatMap(i -> number4.stream()
						.filter(j -> (i+j)%3==0)
						.map(j -> new int[] {i,j}))
				.collect(Collectors.toList());
		for (int[] pair : pairs2) {
			System.out.println("("+pair[0]+","+pair[1]+")");
		}
		
		System.out.println("************************************");
		List<Dish> menu = DishList.getDishs();
		if(menu.stream().anyMatch(Dish::isVegetarian)) {
			System.out.println("some vegetarian");
		}
		if(menu.stream().allMatch(d -> d.getCalories()<1000)) {
			System.out.println("some health");
		}
		if(menu.stream().noneMatch(d -> d.getCalories()>=1000)) {
			System.out.println("some health");
		}
		
		System.out.println("************************************");
		Optional<Dish> dOptional = menu.stream()
		.filter(Dish::isVegetarian)
		.findAny();
		System.out.println(dOptional.get());
		System.out.println(dOptional.isPresent());
		
		System.out.println("************************************");
		List<Integer> numbers5 = Arrays.asList(1,2,3,4,5);
		Optional<Integer> numberOptional = numbers5.stream()
		.map(i -> i*i)		
		.filter(i -> i%3==0)
		.findFirst();
		System.out.println(numberOptional.get());
		System.out.println(numberOptional.isPresent());
		
		System.out.println("************************************");
		int count = DishList.getDishs()
		.stream()
		.map(d -> 1)
		.reduce(0, (a, b) -> a+b);
		System.out.println(count);
		System.out.println(DishList.getDishs().stream().count());
	}

}
